package com.cw.jeeyt.service.impl.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiFacadeModuleInfoBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeModuleInfoDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeModuleInfoDo;
import com.cw.jeeyt.service.intf.api.ApiFacadeModuleInfoService;
import com.cw.jeeyt.service.converter.api.ApiFacadeModuleInfoConverter;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * api接口文档所在项目模块业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeModuleInfoServiceImpl implements ApiFacadeModuleInfoService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeModuleInfoServiceImpl.class);
    @Autowired
    private ApiFacadeModuleInfoDao apiFacadeModuleInfoDao;//api接口文档所在项目模块数据库操作接口

    /**
     * 查询所有api接口文档所在项目模块
     */
    @Override
    public List<ApiFacadeModuleInfoBo> queryApiFacadeModuleInfoList() throws Exception {
        return ApiFacadeModuleInfoConverter.doList2BoList(apiFacadeModuleInfoDao.queryApiFacadeModuleInfoList());
    }

    /**
     * 分页查询api接口文档所在项目模块
     *
     * @param apiFacadeModuleInfoBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeModuleInfoBo> queryByPage(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo, Integer pageNo,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ApiFacadeModuleInfoDo> pageInfo = apiFacadeModuleInfoDao.queryByPage(
            ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo), pageNo, pageSize, order);
        return new PageInfo<>(pageInfo, ApiFacadeModuleInfoConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻api接口文档所在项目模块信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeModuleInfoBo getApiFacadeModuleInfoById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiFacadeModuleInfoConverter.do2Bo(apiFacadeModuleInfoDao.getById(id));
    }

    /**
     * 新增api接口文档所在项目模块
     *
     * @param apiFacadeModuleInfoBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insert(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo) throws Exception {

        if (apiFacadeModuleInfoBo == null) {
            logger.error("api接口文档所在项目模块为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        //校验当前项目下模块是否已存在
        ApiFacadeModuleInfoBo checkResult = this.queryByProjectIdAndModuleCode(apiFacadeModuleInfoBo.getProjectId(),
            apiFacadeModuleInfoBo.getModuleCode());
        if (checkResult != null) {
            throw new Exception("要新增的模块信息已存在：" + apiFacadeModuleInfoBo.getModuleCode());
        }
        ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo);
        apiFacadeModuleInfo.setCreateBy(userVo.getId());
        apiFacadeModuleInfo.setCreateDate(new Date());
        apiFacadeModuleInfo.setUpdateBy(userVo.getId());
        apiFacadeModuleInfo.setUpdateDate(new Date());
        apiFacadeModuleInfo.setId(IdGenerator.nextId());
        apiFacadeModuleInfoDao.insert(apiFacadeModuleInfo);
        ApiFacadeModuleInfoBo result = this.queryByProjectIdAndModuleCode(apiFacadeModuleInfoBo.getProjectId(),
            apiFacadeModuleInfoBo.getModuleCode());
        return result.getId();
    }

    /**
     * 根据项目id及模块编码查询模块信息
     *
     * @param projectId 项目id
     * @param moduleCode 模块编码
     * @return
     * @throws Exception
     */
    @Override
    public ApiFacadeModuleInfoBo queryByProjectIdAndModuleCode(Long projectId, String moduleCode) throws Exception {

        if (null == projectId || StringUtils.isEmpty(moduleCode)) {
            logger.error("--项目id或模块编码不能为空--");
            return null;
        }
        return ApiFacadeModuleInfoConverter.do2Bo(
            apiFacadeModuleInfoDao.queryByProjectIdAndModuleCode(projectId, moduleCode));
    }

    /**
     * 新增api接口文档所在项目模块不为空的字段
     *
     * @param apiFacadeModuleInfoBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insertSelective(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo) throws Exception {

        if (apiFacadeModuleInfoBo == null) {
            logger.error("api接口文档所在项目模块为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeModuleInfoBo checkResult = queryByProjectIdAndModuleCode(apiFacadeModuleInfoBo.getProjectId(),
            apiFacadeModuleInfoBo.getModuleCode());
        if (checkResult != null) {
            throw new Exception("要新增的模块信息已存在：" + apiFacadeModuleInfoBo.getModuleCode());
        }
        ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo);
        apiFacadeModuleInfo.setCreateBy(userVo.getId());
        apiFacadeModuleInfo.setCreateDate(new Date());
        apiFacadeModuleInfo.setUpdateBy(userVo.getId());
        apiFacadeModuleInfo.setUpdateDate(new Date());
        apiFacadeModuleInfo.setId(IdGenerator.nextId());
        apiFacadeModuleInfoDao.insertSelective(apiFacadeModuleInfo);
        ApiFacadeModuleInfoBo result = this.queryByProjectIdAndModuleCode(apiFacadeModuleInfoBo.getProjectId(),
            apiFacadeModuleInfoBo.getModuleCode());
        return result.getId();
    }

    /**
     * 更新api接口文档所在项目模块
     *
     * @param apiFacadeModuleInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo) throws Exception {

        if (apiFacadeModuleInfoBo == null) {
            logger.error("api接口文档所在项目模块为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo);
        apiFacadeModuleInfo.setUpdateBy(userVo.getId());
        apiFacadeModuleInfo.setUpdateDate(new Date());

        apiFacadeModuleInfoDao.update(apiFacadeModuleInfo);
    }

    /**
     * 更新api接口文档所在项目模块不为空的字段
     *
     * @param apiFacadeModuleInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo) throws Exception {

        if (apiFacadeModuleInfoBo == null) {
            logger.error("api接口文档所在项目模块为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo);
        apiFacadeModuleInfo.setUpdateBy(userVo.getId());
        apiFacadeModuleInfo.setUpdateDate(new Date());

        apiFacadeModuleInfoDao.updateSelective(apiFacadeModuleInfo);
    }

    /**
     * 删除api接口文档所在项目模块
     *
     * @param apiFacadeModuleInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeModuleInfoBo apiFacadeModuleInfoBo) throws Exception {

        if (apiFacadeModuleInfoBo == null) {
            logger.error("api接口文档所在项目模块为空");
            return;
        }
        ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(apiFacadeModuleInfoBo);
        apiFacadeModuleInfoDao.delete(apiFacadeModuleInfo);
    }

    /**
     * 根据项目id查询所有api接口文档所在项目模块列表信息
     *
     * @param projectId 项目id
     */
    @Override
    public List<ApiFacadeModuleInfoBo> queryListByProjectId(Long projectId) throws Exception {
        if (null == projectId) {
            logger.error("项目id为空");
            return null;
        }

        return ApiFacadeModuleInfoConverter.doList2BoList(apiFacadeModuleInfoDao.queryListByProjectId(projectId));
    }

    /**
     * 批量保存接口模块信息
     */
    @Override
    public void saveApiModuleInfoBat(List<ApiFacadeModuleInfoBo> apiFacadeModuleInfoList) throws Exception {
        if (apiFacadeModuleInfoList == null || apiFacadeModuleInfoList.isEmpty()) {
            logger.error("接口模块信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        List<ApiFacadeModuleInfoDo> moduleInfoList = new ArrayList<ApiFacadeModuleInfoDo>();
        for (ApiFacadeModuleInfoBo moduleInfoInfo : apiFacadeModuleInfoList) {
            ApiFacadeModuleInfoDo apiFacadeModuleInfo = ApiFacadeModuleInfoConverter.bo2Do(moduleInfoInfo);
            apiFacadeModuleInfo.setCreateBy(userVo.getId());
            apiFacadeModuleInfo.setCreateDate(new Date());
            apiFacadeModuleInfo.setUpdateBy(userVo.getId());
            apiFacadeModuleInfo.setUpdateDate(new Date());
            moduleInfoList.add(apiFacadeModuleInfo);
        }
        Integer result = apiFacadeModuleInfoDao.insertBat(moduleInfoList);
    }

    /**
     * 根据模块编码查询模块信息
     */
    @Override
    public List<ApiFacadeModuleInfoBo> queryListByModuleCodes(List<String> moduleCodes) throws Exception {
        if (moduleCodes == null || moduleCodes.isEmpty()) {
            logger.error("模块编码为空");
            return null;
        }
        return ApiFacadeModuleInfoConverter.doList2BoList(apiFacadeModuleInfoDao.queryListByModuleCodes(moduleCodes));
    }
}
